package com.woniuxy.productServer.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.util.Result;
import com.woniuxy.productServer.dto.sessionTicket.SessionTicketDto;
import com.woniuxy.productServer.exception.sessionTicket.SessionTicketException;
import com.woniuxy.productServer.exception.sessionTicket.SessionTicketExceptionCode;
import com.woniuxy.productServer.form.sessionTicket.SessionTicketLoadForm;
import com.woniuxy.productServer.model.MovieSession;
import com.woniuxy.productServer.model.SessionTicket;
import com.woniuxy.productServer.service.MovieSessionService;
import com.woniuxy.productServer.service.SessionTicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年03月20日
 */
@RestController
@Api("电影票")
@RequestMapping("/sessionTicket")


public class SessionTicketController {
@Autowired
private SessionTicketService sessionTicketService;
@Autowired
private MovieSessionService movieSessionService;

    @PostMapping("/load")
    @ApiOperation("查询电影票")
    public Result load(@RequestBody SessionTicketLoadForm form){
        PageHelper.startPage(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?20: form.getPageSize());
        if (form.getSessionId()==null) throw new SessionTicketException(SessionTicketExceptionCode.SESSION_TICKET_NOT_SESSION_ID);
        List<SessionTicket> list = sessionTicketService.list(new QueryWrapper<SessionTicket>().eq("session_id", form.getSessionId()));
        PageInfo<SessionTicket> pageInfo = new PageInfo<>(list);
        MovieSession session = movieSessionService.getById(form.getSessionId());
        ArrayList<SessionTicketDto> sessionTicketDtos = new ArrayList<>();
        for (SessionTicket ticket : list) {
            SessionTicketDto ticketDto = BeanUtil.toBean(ticket, SessionTicketDto.class);
            ticketDto.setCinemaName(session.getCinemaName());
            ticketDto.setSeatDetail("放映厅："+session.getCinemaName()+"\n"+"座位："+ticketDto.getSeatRow()+"行 "+ticketDto.getSeatCol()+"列");
            sessionTicketDtos.add(ticketDto);
        }
        PageInfo<SessionTicketDto> dtoPageInfo = new PageInfo<>(sessionTicketDtos);
        dtoPageInfo.setPages(pageInfo.getPages());
        dtoPageInfo.setPageNum(pageInfo.getPageNum());
        dtoPageInfo.setPageSize(pageInfo.getPageSize());
        dtoPageInfo.setTotal(pageInfo.getTotal());
        return Result.getPageInfoResult(SessionTicketDto.class).setMsg("查询电影票成功").setData(dtoPageInfo);
    }

    @PostMapping("/loadSessionTicketsByIds")
    public Result loadSessionTicketsByIds(@RequestBody List<Integer> ticketIds){
        List<SessionTicket> sessionTickets = sessionTicketService.listByIds(ticketIds);
        return Result.getListResult(SessionTicket.class).setData(sessionTickets).setMsg("成功");
    }

    @PostMapping("/updateSessionTicketsByIds")
    public Result<Boolean> updateSessionTicketsByIds(@RequestBody List<Integer> ticketIds){
        Boolean bool= sessionTicketService.updateSessionTicketsByIds(ticketIds);
        return Result.getResult(Boolean.class).setMsg("成功").setData(bool);
    }

    public static void main(String[] args) {
        System.out.println("1"+"\n"+"2");
    }

}

